import uuid

from django.db import models

from django.contrib.auth import get_user_model

# 从系统中获取自己定义的user表
User = get_user_model()


# Create your models here.

class ArticleModel(models.Model):
    """
    文章/博客model
    """
    uuid = models.CharField(max_length=100, default=uuid.uuid4, unique=True, verbose_name='文章id')
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者名称')
    title = models.CharField(max_length=100, default='', null=False, blank=False, verbose_name='博客标题')
    content = models.TextField(null=False, blank=False, verbose_name='博客内容')
    read_num = models.IntegerField(default=0, verbose_name='阅读数')
    comment_num = models.IntegerField(default=0, verbose_name='评论数')
    fav_num = models.IntegerField(default=0, verbose_name='收藏数')
    click_num = models.IntegerField(default=0, verbose_name='点赞数')
    # person_category = models.ManyToManyField('PersonCategoryModel', verbose_name='个人文章分类')
    # tag = models.ManyToManyField('TagModel', verbose_name='文章标签')
    type = models.ForeignKey('TypeCategoryModel', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='文章类型')
    category = models.ForeignKey('BlogCategoryModel', null=True, blank=True, on_delete=models.SET_NULL,
                                 verbose_name='文章分类')
    create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间')
    update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间')

    class Meta(object):
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class PersonCategoryModel(models.Model):
    """
    个人博客分类
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    name = models.CharField(max_length=20, verbose_name='分类名')
    create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间')
    update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间')

    class Meta(object):
        verbose_name = '个人博客分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class TagModel(models.Model):
    """
    文章标签
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    name = models.CharField(max_length=20, verbose_name='分类名')
    create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间')
    update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间')

    class Meta(object):
        verbose_name = '文章标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class BlogComments(models.Model):
    """
    文章评论
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    article = models.ForeignKey(ArticleModel, on_delete=models.CharField, verbose_name='文章')
    comment = models.CharField(max_length=500, null=False, blank=False, verbose_name='评论内容')
    create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间')
    update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间')

    class Meta(object):
        verbose_name = '文章评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user


class BlogCategoryModel(models.Model):
    """
    博客分类
    """
    choice_type = (
        ('web', 'web前端'),
        ('java', 'java开发'),
        ('python', 'python开发'),
        ('android', '安卓'),
        ('other', '其它')
    )
    category = models.CharField(max_length=30, choices=choice_type, default='python', verbose_name='文章分类')
    create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间')
    update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间')

    class Meta(object):
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category


class TypeCategoryModel(models.Model):
    """
    文章类型
    """

    choice_type = (
        ('original', '原创'),
        ('reprinted', '转载'),
    )
    type = models.CharField(max_length=30, choices=choice_type, default='original', verbose_name='文章类型')
    create_at = models.DateTimeField(auto_now_add=True, null=False, verbose_name='添加时间')
    update_at = models.DateTimeField(auto_now=True, null=False, verbose_name='修改时间')

    class Meta(object):
        verbose_name = '文章类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.type
